﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using ProfileManager.DataAccess;
using ProfileManager.Helpers;

namespace ProfileManager.Forms
{
    public partial class UserInfoForm : MasterForm
    {
        public bool IsNewUser { get; set; }

        public UserInfoForm()
        {
            InitializeComponent();

            ShowCustomLeftButton = false;
            ShowCustomRightButton = false;
        }

        private bool FormValidation()
        {
            bool isValidated = false;
            if (!string.IsNullOrEmpty(tbLogin.Text.Trim()))
            {
                if (!string.IsNullOrEmpty(tbEmail.Text.Trim()))
                {
                    if (Regex.Match(tbEmail.Text.Trim(), RegexPatterns.PatternEmail).Success)
                    {
                        string password = tbPassword.Text.Trim();
                        string passwordRepeated = tbPasswordRepeat.Text.Trim();
                        if (!string.IsNullOrEmpty(password) && !string.IsNullOrEmpty(passwordRepeated) && password.Equals(passwordRepeated) && password.Length >= 8)
                        {
                            isValidated = true;
                        }
                    }
                }
            }
            return isValidated;
        }

        private void UserInfoFormClickOk(object sender, EventArgs e)
        {
            if (FormValidation())
            {
                if (IsNewUser)
                {
                    UserInfo userInfo = new UserInfo
                        { Login = tbLogin.Text.Trim(), Email = tbEmail.Text.Trim(), Password = tbPassword.Text.Trim() };
                    userInfo.Insert();

                    if (userInfo.Id != Guid.Empty)
                    {
                        DialogResult = DialogResult.OK;
                    }
                }
            }
        }

        private void UserInfoForm_Load(object sender, EventArgs e)
        {
            if (IsNewUser)
            {
                lRegistrationDateValue.Text = DateTime.Now.ToShortDateString();
                HeaderText = "Регистрация нового пользователя";
                Text = "Регистрация";
            }
            else
            {
                //lRegistrationDateValue.Text = DateTime.Now.ToShortDateString();
                HeaderText = "Данные пользователя";
                Text = "Данные пользователя";
            }
        }
    }
}
